//
//  TVRemoteView.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/5.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "TVRemoteView.h"
#import "NumberMenuView.h"
#import "HomeCollectionViewCell.h"
@interface TVRemoteView ()<NumberMenuViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) UICollectionView *collectionView;
@property(nonatomic,strong) NSArray *dataSource;


@end


@implementation TVRemoteView

#pragma mark --- 代理
- (void)numberMenuViewSelectedIndex:(NSInteger)index
{
    if ([self.delegate respondsToSelector:@selector(TVRemoteViewSelectedIndex:)])
    {
        [self.delegate TVRemoteViewSelectedIndex:index];
    }
}

#pragma mark - UICollectionViewDelegate,UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *array = self.dataSource.firstObject;
    return array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeCollectionViewCell *cell = [HomeCollectionViewCell cellWithCollectionView:collectionView forIndexPath:indexPath];
    NSArray *titleArray = self.dataSource.firstObject;
    NSArray *imageArray = self.dataSource[1];
    
    cell.iconImageView.image = imageWithName(imageArray[indexPath.row]);
    cell.titleLabel.text = titleArray[indexPath.row];
    cell.badgeLabel.hidden = YES;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = (HomeCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    NSArray *imageArray = self.dataSource.lastObject;
    cell.iconImageView.image = imageWithName(imageArray[indexPath.row]);
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = (HomeCollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    NSArray *imageArray = self.dataSource[1];
    cell.iconImageView.image = imageWithName(imageArray[indexPath.row]);
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.delegate respondsToSelector:@selector(TVRemoteViewSelectedIndex:)])
    {
        [self.delegate TVRemoteViewSelectedIndex:indexPath.row];
    }
}


#pragma mark --- 懒加载
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(px1080Width(1000)/4 - 8 , px1920Height(601)/2 - 4);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(6, 6, 6, 6);
        _collectionView = ({
            UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
            collectionView.dataSource = self;
            collectionView.delegate = self;
            collectionView.scrollEnabled = NO;
            collectionView.backgroundColor = [UIColor clearColor];
            [self addSubview:collectionView];
            collectionView;
        });
        
        
        
    }return _collectionView;
}

- (NumberMenuView *)numberMenuView
{
    if(!_numberMenuView)
    {
        UIView *superView = self.scrollView;
        _numberMenuView = [[NumberMenuView alloc]init];
        [superView addSubview:_numberMenuView];
        _numberMenuView.delegate = self;
    }
    return _numberMenuView;
}

- (UIScrollView *)scrollView
{
    if(!_scrollView)
    {
        UIView *superView = self;
        _scrollView = [[UIScrollView alloc]init];
        [superView addSubview:_scrollView];
        _scrollView.contentSize = CGSizeMake(0, px1920Height(1800));
        _scrollView.scrollEnabled = YES;
        _scrollView.userInteractionEnabled = YES;
    }
    return _scrollView;
}

- (NSArray *)dataSource
{
    if (!_dataSource)
    {
        _dataSource = @[@[@"菜单",@"音效",@"睡眠",@"退出"],@[@"icon_menu",@"icon_sound effect",@"icon_sleep",@"icon_quit"],@[@"icon_menu_pre",@"icon_sound effect_pre",@"icon_sleep_pre",@"icon_quit_pre"]];
    }
    return _dataSource;
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self;
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView.mas_top).with.offset(px1920Height(250));
        make.left.right.bottom.mas_equalTo(superView);
    }];
    
    UIView *scrollContentView = [[UIView alloc] init];
    [self.scrollView addSubview:scrollContentView];
    scrollContentView.frame = CGRectMake(0, 0, SCREEN_WIDTH, px1920Height(2100));
    
    [scrollContentView addSubview:self.numberMenuView];
    [scrollContentView addSubview:self.collectionView];
    
    [self.numberMenuView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(scrollContentView.mas_top);
        make.left.mas_equalTo(scrollContentView.mas_left).with.offset(px1080Width(90));
        make.right.mas_equalTo(scrollContentView.mas_right).with.offset(-px1080Width(90));
        make.height.mas_equalTo(px1920Height(1452));
    }];
    
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.numberMenuView.mas_bottom).with.offset(px1920Height(40));
        make.left.right.mas_equalTo(scrollContentView);
        make.height.mas_equalTo(px1920Height(601));
        //        make.centerX.mas_equalTo(scrollContentView.mas_centerX);
//        make.size.mas_equalTo(CGSizeMake(px1080Width(1000), px1920Height(601)));
    }];
    
}

@end
